var $;
var path=	getRootPath();
var global_visit_url_prefix = "";//全局访问路径前缀
layui.use(['form'], function() {
					$ = layui.jquery;   
	});  

function getRootPath(){
    //获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
    var curWwwPath=window.document.location.href;
    //获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
    var pathName=window.document.location.pathname;
    var pos=curWwwPath.indexOf(pathName);
    //获取主机地址，如： http://localhost:8083
    var localhostPaht=curWwwPath.substring(0,pos);
    //获取带"/"的项目名，如：/uimcardprj
    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
 return(localhostPaht+projectName);
}


/*需要layiui的依赖*/
//写入到Cookie
//name:cookie名称  value:cookie值 
/***
	*读取指定的Cookie值 readCookie("id");
	*@param {string} cookieName Cookie名称
	*/
	function getCookieVal(cookieName) {
	    var theCookie = "" + document.cookie;
	    var ind = theCookie.indexOf(cookieName);
	    if(ind==-1 || cookieName=="") return "";
	    var ind1 = theCookie.indexOf(';',ind);
	    if(ind1==-1) ind1 = theCookie.length;
	    /*读取Cookie值*/
	    return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
	}
	
	/***
	* 设置Cookie值 setCookie("id",1);
	* @param {string} cookieName Cookie名称
	* @param {string} cookieValue Cookie值
	* @param {number} nDays Cookie过期天数
	*/
	function SetCookie(cookieName, cookieValue,cookietime) {
	    /*当前日期*/
	    var today = new Date();
	    /*Cookie过期时间*/
	    var expire = new Date();
	    /*如果未设置nDays参数或者nDays为0，取默认值1*/
	    //if(nDays == null || nDays == 0) nDays = 1;
	    /*计算Cookie过期时间【 3600000 * 24  为一天】*/
	    expire.setTime(today.getTime() + cookietime); //5分钟
	    document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" +      expire.toGMTString();
	}
	
	/***
	* 删除cookie中指定变量函数  
	* @param {string} $name Cookie名称
	*/    
    function deleteCookie($name){    
         var myDate=new Date();    
         myDate.setTime(-1000);//设置时间    
         document.cookie=$name+"=''; expires="+myDate.toGMTString();                
    }
	
	/***
	* 删除cookie中所有定变量函数  
	* @param {string} cookieName Cookie名称
	* @param {string} cookieValue Cookie值
	* @param {number} nDays Cookie过期天数
	*/  
    function clearCookie(){    
         var myDate=new Date();    
         myDate.setTime(-1000);//设置时间    
         var data=document.cookie;    
         var dataArray=data.split("; ");    
         for(var i=0;i<dataArray.length;i++){    
              var varName=dataArray[i].split("=");    
              document.cookie=varName[0]+"=''; expires="+myDate.toGMTString();    
         }    
    }

//获取用户信息
function getSharreTieba_LoginUser(){

	var data=  getSharreTieba_token_LoginInfo();
	if(data==null){
		return null;
	}
	return data.user;
}

//获取认证信息
function getSharreTieba_token_LoginInfo(){
	 var stf= getCookieVal("SharreTieba_token_LoginInfo");
	   if(stf==null||stf=="" ){
	   	return null;
	   }else{
	   	 var data=JSON.parse(stf)
	   	 return data;
	   }
}

//获取认证Token
function getSharreTieba_LoginToken(){

	   var data=  getSharreTieba_token_LoginInfo();
	   if(data==null){
	   	return null;
	   }
		 return data.LoginToken;
}
  
//匹配权限url 重定向  跳转页面 
//url 跳转的url
function windowlocationhref(url){
  var SharreTieba  = getSharreTieba_token_LoginInfo();
    if(SharreTieba==null){
    	layer.msg("权限不足", {
					icon: 5,
					anim: 6
		});
    	return;
    }
	window.location.href=path+url;
  
}
  
//页面加载权限匹配
function pageloadauthority(url){
  var SharreTieba  = getSharreTieba_token_LoginInfo();
  
   /*if(SharreTieba==null||SharreTieba==undefined){
    layer.open({
        type: 1
        ,title: '权限不足' //不显示标题栏
        ,closeBtn: false
        ,area: '300px;'
        ,shade: 0.8
        ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
        ,btn: ['登录'] 
        ,btnAlign: 'c'
        ,moveType: 1 //拖拽模式，0或者1
        ,content: '<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">请进行登录或联系管理员进行进行授权!<br>SharreTieba,欢迎您的加入! ^_^</div>'
        ,success: function(layero){
          var btn = layero.find('.layui-layer-btn');
              btn.click(function(){
		      window.location.href=path+"/login.html"
		  }); 
        }
      });
		return false;
   }
     
 for(var i=0;i<(SharreTieba.sysTags).length;i++ ){
     console.log(SharreTieba.sysTags[i].url)
  	if(url==SharreTieba.sysTags[i].url){
  	        return true;
  	}
  } 
         
  for(var i=0;i<(SharreTieba.sysTags).length;i++ ){
     console.log(SharreTieba.sysTags[i].url)
   if(url!=SharreTieba.sysTags[i].url){
    layer.open({
        type: 1
        ,title: '权限不足' //不显示标题栏
        ,closeBtn: false
        ,area: '300px;'
        ,shade: 0.8
        ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
        ,btn: ['登录'] 
        ,btnAlign: 'c'
        ,moveType: 1 //拖拽模式，0或者1
        ,content: '<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">请进行登录或联系管理员进行进行授权!<br>SharreTieba,欢迎您的加入! ^_^</div>'
        ,success: function(layero){
          var btn = layero.find('.layui-layer-btn');
              btn.click(function(){
		      window.location.href=path+"/login.html"
		  }); 
        }
      });
		return false;
  	}
  }*/
}

/*登录token验证*/
function atuLoginToken(){
	
 var logintoken=getSharreTieba_LoginToken();
	 console.log(logintoken)
	 if(logintoken!=null ){
	 	 $.ajax({
				  "async": false,
				  "crossDomain": true,
				 "url": "/permission/autLoginToken",
				 "method": "POST",
					data:{'LoginToken':logintoken},
					success:function(json){
						if(json.code==200){
							 layer.open({
							        type: 1
							        ,title: '未登录' //不显示标题栏
							        ,closeBtn: false
							        ,area: '300px;'
							        ,shade: 0.8
							        ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
							        ,btn: ['登录'] 
							        ,btnAlign: 'c'
							        ,moveType: 1 //拖拽模式，0或者1
							        ,content: '<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">您还没有登录，请进行登录操作!<br>SharreTieba,欢迎您的加入! ^_^</div>'
							        ,success: function(layero){
							          var btn = layero.find('.layui-layer-btn');
							              btn.click(function(){
									      window.location.href=path+"/login.html"
									  }); 
							        }
							      });
									return;
						}
					}
				});
	 }
}


/*标签权限匹配*/
function tagloadauthority(url){
var SharreTieba  = getSharreTieba_token_LoginInfo();
   if(SharreTieba==null||SharreTieba==undefined){
		return false;
   }
  for(var i=0;i<(SharreTieba.sysTags).length;i++ ){
  	if(url==SharreTieba.sysTags[i].url){
  			return true;
  	}
  }
}  
